package com.ks.repair.service.impl;

import com.baomidou.mybatisplus.enums.SqlLike;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.ks.core.base.OperationContext;
import com.ks.core.constant.Const;
import com.ks.core.constant.DatePanel;
import com.ks.core.dto.OperateBase;
import com.ks.core.dto.SearchCriteriaBase;
import com.ks.core.util.ToolUtil;
import com.ks.repair.controller.dto.RepairBillRequest;
import com.ks.repair.dao.BRepairBillExMapper;
import com.ks.repair.dao.BRepairBillTemplateMapper;
import com.ks.repair.entity.BRepairBill;
import com.ks.repair.dao.BRepairBillMapper;
import com.ks.repair.entity.BRepairBillEx;
import com.ks.repair.entity.BRepairBillTemplate;
import com.ks.repair.entity.BRepairBillUserAddressCount;
import com.ks.repair.entity.vo.BRepairBillInfo;
import com.ks.repair.entity.vo.BRepairBillParam;
import com.ks.repair.entity.vo.RepairEx;
import com.ks.repair.service.IBRepairBillService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.ks.repair.service._ServiceBase;
import org.apache.ibatis.session.RowBounds;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * <p>
 * 报修-会员填写的报修单 服务实现类
 * </p>
 *
 * @author kingdee123
 * @since 2020-12-15
 */
@Service
public class BRepairBillServiceImpl extends _ServiceBase<BRepairBillMapper, BRepairBill> implements IBRepairBillService {
    @Resource
    private BRepairBillMapper bRepairBillMapper;

    @Resource
    private BRepairBillExMapper bRepairBillExMapper;

    @Resource
    private BRepairBillTemplateMapper bRepairBillTemplateMapper;


    @Override
    public Map<String, Object> getBRepairBillUserCount(OperationContext context) {
        Map<String, Object> map = new HashMap<>();
        EntityWrapper<BRepairBill> wrapper = new EntityWrapper<>();
        BRepairBill repairBill = new BRepairBill();
        repairBill.setDelMark(Const.DEL_FLAG_NORMAL);
        repairBill.setTenantID(context.getTenantId().intValue());
        wrapper.setEntity(repairBill);
        Integer integer = bRepairBillMapper.selectCount(wrapper);
        map.put("CardName", "累计收到商机");
        map.put("DataCount", "1");
        List list = new ArrayList();
        DatePanel panel = new DatePanel();
        panel.setDataTime(new Date());
        panel.setDataValue(integer);
        panel.setValueType("STR");
        list.add(panel);
        map.put("date", list);
        return map;
    }

    @Override
    public Map<String, Object> getServiceTypeUserCount(OperationContext context) {
        Map<String, Object> map = new HashMap<>();
        List<BRepairBillUserAddressCount> bRepairBillUserCount = bRepairBillMapper.getServiceTypeUserCount(context.getTenantId().intValue());
        List<DatePanel> list = new ArrayList<>();
        if (ToolUtil.isNotEmpty(bRepairBillUserCount)) {
            for (int i = 0; i < bRepairBillUserCount.size(); i++) {
                DatePanel panel = new DatePanel();
                panel.setDataTime(new Date());
                panel.setDataName(bRepairBillUserCount.get(i).getName());
                panel.setDataValue(bRepairBillUserCount.get(i).getCount());
                panel.setValueType("STR");
                list.add(panel);
            }
        }
        map.put("CardName", "报修类别雷达图");
        map.put("DataCount", bRepairBillUserCount.size());
        map.put("date", list);
        return map;
    }

    @Override
    public Map<String, Object> getBRepairBillUserAddressCount(OperationContext context) {
        Map<String, Object> map = new HashMap<>();
        List<BRepairBillUserAddressCount> bRepairBillUserCount = bRepairBillMapper.getBRepairBillUserCount(context.getTenantId().intValue());
        List<DatePanel> list = new ArrayList<>();
        if (ToolUtil.isNotEmpty(bRepairBillUserCount)) {
            for (int i = 0; i < bRepairBillUserCount.size(); i++) {
                DatePanel panel = new DatePanel();
                panel.setDataTime(new Date());
                panel.setDataName(bRepairBillUserCount.get(i).getName());
                panel.setDataValue(bRepairBillUserCount.get(i).getCount());
                panel.setValueType("STR");
                list.add(panel);
            }
        }
        map.put("CardName", "报修省市占比图");
        map.put("DataCount", bRepairBillUserCount.size());
        map.put("date", list);
        return map;
    }

    @Override
    public Map<String, Object> selectRepairBillList(OperationContext context, SearchCriteriaBase searchCriteriaBase) {
        ArrayList<SearchCriteriaBase.matchField> fields = searchCriteriaBase.getFields();
        BRepairBillParam bRepairBill = new BRepairBillParam();
        //搜索条件
        if (ToolUtil.isNotEmpty(fields)) {
            for (SearchCriteriaBase.matchField field : fields) {
                if ("name".equals(field.getName())) {
                    if (ToolUtil.isNotEmpty(field.getValue())) {
                        bRepairBill.setICustName(field.getValue());
                    }
                }
                if ("iphone".equals(field.getName())) {
                    if (ToolUtil.isNotEmpty(field.getValue())) {
                        bRepairBill.setICustPhone(field.getValue());
                    }
                }
                if ("tenantID".equals(field.getName())) {
                    if (ToolUtil.isNotEmpty(field.getValue())) {
                        bRepairBill.setTenantID(Integer.valueOf(field.getValue()));
                    }
                }
                if ("repairState".equals(field.getName())) {
                    if (ToolUtil.isNotEmpty(field.getValue())) {
                        bRepairBill.setSRepairState(Integer.valueOf(field.getValue()));
                    }
                }
                if ("area".equals(field.getName())) {
                    if (ToolUtil.isNotEmpty(field.getValue())) {
                        String[] areas = field.getValue().split(",");
                        for (int i = 0; i < areas.length; i++) {
                            if (i == 0) {
                                bRepairBill.setIProvinceId(Integer.valueOf(areas[i]));
                            } else if (i == 1) {
                                bRepairBill.setICityId(Integer.valueOf(areas[i]));
                            } else if (i == 2) {
                                bRepairBill.setIAreaId(Integer.valueOf(areas[i]));
                            }

                        }
                    }
                }
                if ("repairTime".equals(field.getName())) {
                    if (ToolUtil.isNotEmpty(field.getValue())) {
                        String[] time = field.getValue().split(",");
                        bRepairBill.setStartTime(time[0] + " 00:00:00");
                        bRepairBill.setEndTime(time[1] + " 23:59:59");
                    }
                }
            }
        }
        if (!isSystem(context)) {
            bRepairBill.setTenantID(context.getTenantId().intValue());
        }

        Page limit = new Page(searchCriteriaBase.getPageIndex(), searchCriteriaBase.getLimit());
        List<BRepairBillInfo> bRepairBills = bRepairBillMapper.selectBRepairBillInfo(limit, bRepairBill);
        Integer count = bRepairBillMapper.selectBRepairBillCount(bRepairBill);
        Map<String, Object> resultMap = this.isResultMap(bRepairBills, searchCriteriaBase);
        resultMap.put("count", count);
        return resultMap;
    }

    @Override
    public int addRepairBill(OperationContext context, BRepairBill bRepairBill, List<BRepairBillEx> bRepairBillExList) {
        bRepairBill.setTenantID(context.getTenantId().intValue());
        bRepairBill.setOrgID(context.getOrgId().intValue());
        bRepairBill.setOrgSubID(context.getOrgSubId().intValue());
        bRepairBill.setCreateUserID(context.getUserId().intValue());
        bRepairBill.setDataType("bRepairBill");
        bRepairBill.setIRepairTime(new Date());
        Integer i = bRepairBillMapper.insert(bRepairBill);
        for (BRepairBillEx bRepairBillEx : bRepairBillExList) {
            bRepairBillEx.setTenantID(context.getTenantId().intValue());
            bRepairBillEx.setOrgID(context.getOrgId().intValue());
            bRepairBillEx.setOrgSubID(context.getOrgSubId().intValue());
            bRepairBillEx.setCreateUserID(context.getUserId().intValue());
            bRepairBillEx.setIRepairBillId(bRepairBill.getId());
            bRepairBillEx.setDataType("TENANT");
            bRepairBillExMapper.insert(bRepairBillEx);
        }
        return i;
    }

    @Override
    public int updateRepairBill(OperationContext context, BRepairBill bRepairBill) {
        Integer i = bRepairBillMapper.updateById(bRepairBill);
        return i;
    }

    @Override
    public int deleteRepairBill(OperationContext context, BRepairBill bRepairBill) {
        Integer i = bRepairBillMapper.deleteById(bRepairBill);
        return i;
    }

    @Override
    public Map<String, Object> selectRepairBill(OperationContext context, RepairBillRequest repairBillRequest) {
        Page page = new Page(0, 10);
        BRepairBillParam bRepairBillParam = new BRepairBillParam();
        bRepairBillParam.setId(repairBillRequest.getId());
        List<BRepairBillInfo> bRepairBillInfos = bRepairBillMapper.selectBRepairBillInfo(page, bRepairBillParam);


        //查询扩展字段
        BRepairBillTemplate bRepairBillTemplate = new BRepairBillTemplate();
        bRepairBillTemplate.setId(repairBillRequest.getId());
        List<RepairEx> bRepairBillTemplates = bRepairBillTemplateMapper.RepairExInfo(bRepairBillTemplate);

        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("repairBill", bRepairBillInfos.get(0));
        resultMap.put("repairBillex", bRepairBillTemplates);
        return resultMap;
    }

    @Override
    public Map<String, Object> selectRepairList(OperationContext context, SearchCriteriaBase searchCriteriaBase) {
        Map<String, Object> map = new HashMap<>();
        EntityWrapper<BRepairBill> wrapper = new EntityWrapper<>();
        BRepairBill repairBill = new BRepairBill();
        repairBill.setCreateUserID(context.getUserId().intValue());
        repairBill.setTenantID(context.getTenantId().intValue());
        repairBill.setDelMark(Const.DEL_FLAG_NORMAL);
        wrapper.setEntity(repairBill);
        Integer integer = bRepairBillMapper.selectCount(wrapper);
        map.put("count", integer);
        List<BRepairBill> bRepairBills = bRepairBillMapper.selectPage(new Page(searchCriteriaBase.getPageIndex(), searchCriteriaBase.getLimit()), wrapper);
        map.put("list", bRepairBills);
        return map;
    }

    @Override
    public int delRepairBillList(OperationContext context, OperateBase operateBase) {
        ArrayList<String> idList = operateBase.getIdList();
        int count = 0;
        for (String id : idList) {
            BRepairBill bRepairBill = bRepairBillMapper.selectById(id);
            bRepairBill.setDelMark(Const.DEL_FLAG_DEL);
            Integer i = bRepairBillMapper.updateById(bRepairBill);
            count += i;
        }
        return count;
    }

    @Override
    public int addRepairNo(OperationContext context, OperateBase operateBase) {
        ArrayList<String> idList = operateBase.getIdList();
        int count = 0;
        for (String id : idList) {
            BRepairBill bRepairBill = bRepairBillMapper.selectById(id);
            bRepairBill.setIBusiness(getBillNo(bRepairBill));
            bRepairBill.setSWorkState(Const.DEL_FLAG_DEL);
            Integer i = bRepairBillMapper.updateById(bRepairBill);
            count += i;
        }
        return count;
    }

    private String getBillNo(BRepairBill bRepairBill) {
        StringBuffer billNo = new StringBuffer();
        billNo.append("BX-");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String format = sdf.format(new Date());
        billNo.append(format + "-");
        billNo.append(bRepairBill.getId());
        return billNo.toString();
    }

    public Map<String, Object> isResultMap(List list, SearchCriteriaBase searchCriteriaBase) {
        Map<String, Object> map = new HashMap<>();
        map.put("count", list.size());
        map.put("limit", searchCriteriaBase.getLimit());
        map.put("pageIndex", searchCriteriaBase.getPageIndex());
        map.put("hasMore", false);
        map.put("items", list);
        return map;
    }
}
